<template>
  <div>
    <el-form :model="tableData" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-card class="box-card">
        <span>基本信息</span>
        <div class="text item">
          <el-form-item label="订单号">
            <el-input v-model="tableData.orderNumber" disabled></el-input>
          </el-form-item>
          <el-form-item label="申请时间">
            <el-input v-model="tableData.addTime" disabled></el-input>
          </el-form-item>

          <el-form-item label="订单状态" >
            <el-input v-model="tableData.statusString" disabled></el-input>
          </el-form-item>
          <el-form-item label="汽车总数量">
            <el-input v-model="tableData.carSumNumber" disabled></el-input>
          </el-form-item>
          <el-form-item label="融资经销商">
            <el-input type="textarea" v-model="tableData.dealerName" disabled></el-input>
          </el-form-item>
          <el-form-item label="采购总金额(元)">
            <el-input type="textarea"  :value="tableData.purchaseSumMoney| toFixed2" disabled></el-input>
            <span class="convertCurrency">{{tableData.purchaseSumMoney|convertCurrency}}</span>
          </el-form-item>
          <el-form-item label="融资总金额(元)">
            <el-input type="textarea"  :value="tableData.mortgageSumMoney| toFixed2" disabled></el-input>
            <span class="convertCurrency">{{tableData.mortgageSumMoney|convertCurrency}}</span>
          </el-form-item>
          <el-form-item label="融资比例(%)">
            <el-input :value="(((tableData.mortgageSumMoney)/(tableData.purchaseSumMoney)*100-0).toFixed(2)||'')+'%'" disabled></el-input>
          </el-form-item>
        </div>
      </el-card>
    </el-form>
    <el-form :model="tableData" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>车辆信息</span>
        </div>
        <el-collapse v-for='(item,index) in tableData.carTypes' :key='index' @change="handleChange">
          <el-collapse-item :title="item.carName" name="1">
            <div class="text item">
              <el-form-item label="汽车车型">
                <el-input v-model="item.carName" disabled></el-input>
              </el-form-item>
              <el-form-item label="指导价(元)">
                <el-input v-model="item.guidePrice" disabled></el-input>
                <span class="convertCurrency">{{item.guidePrice|convertCurrency}}</span>
              </el-form-item>
              <el-form-item label="配置">
                <el-input type='textarea' v-model="item.carConfig" disabled></el-input>
              </el-form-item>
              <el-form-item label="采购单价(元)">
                <el-input :value="item.carPurchasePrice| toFixed2" disabled ></el-input>
                <span class="convertCurrency">{{item.carPurchasePrice|convertCurrency}}</span>
              </el-form-item>
              <el-form-item label="汽车数量">
                <el-input v-model="item.carCount" disabled></el-input>
              </el-form-item>
              <el-form-item label="采购金额(元)">
                <el-input :value="item.carPurchaseMoney| toFixed2" disabled  ></el-input>
                <span class="convertCurrency">{{item.carPurchaseMoney|convertCurrency}}</span>
              </el-form-item>

            <el-form-item label="融资金额(元)" :prop="'carTypes.' + index + '.carMortgageMoney'"
              :rules="inspect(true,'融资金额',decimalTwo)">
              <el-input v-number='2' v-model.number="item.carMortgageMoney" :disabled='dialogSendData.isAdd' ></el-input>
              <span class="convertCurrency">{{item.carMortgageMoney|convertCurrency}}</span>
            </el-form-item>
              <el-form-item label="采购合同">
                <qiniu  :qiniuImage="item.carContractPics"  :id="item.carContractPics.id" :canDelete="false"></qiniu>
              </el-form-item>
            </div>

          </el-collapse-item>
        </el-collapse>
      </el-card>
      <el-card class="box-card">
        <el-form-item label="资源审核意见" prop="resourceAuditOpinion">
          <el-input type="textarea" v-model="tableData.resourceAuditOpinion" placeholder="必填，100字限制。" :maxlength='100' :disabled='dialogSendData.isAdd'></el-input>
        </el-form-item>
      </el-card>
      <el-form-item class='align'>
        <el-button type="primary" @click="resetForm('ruleForm')">返回</el-button>
        <el-button type="success" @click="submitForm('ruleForm')" v-if='!dialogSendData.isAdd'>提交</el-button>
      </el-form-item>
    </el-form>
  
  </div>
</template>

<script>
  import qiniu from '@/views/qiniu/upload';
  import {
    require,
    prop,
    decimalTwo,
    muchVerification,
  } from '@/utils/rules.js' //表单验证规则
  import agentHttp from '@/utils/agentHttp.js'
  import {qiniuArray} from '@/utils/index.js'
  export default {
    data() {
      return {
        tableData: {
          carMortgageMoney: '',
        },

        rules: {
          allocation: [require("配置")],
          resourceAuditOpinion: [{
            required: true,
            message: "资源审核意见不能为空",
            trigger: "change"
          }]
        },
        activeNames: [], // 汽车选项卡  
        qiniu: [],
      };
    },
    methods: {      
      submitForm(formName) {      //提交数据
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.takeData()
            //发请求了
            agentHttp.postJson(`adminApi/admin/mortgage_order/first_order/sumbit`, this.takeData()).then((res) => {
              if(res.success){
                this.dialogSendData.dialogFormVisible = false;
                this.$message.success(res.data||"修改成功");
                this.$emit('Refresh',null);
              }
            })
        
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
        this.dialogSendData.dialogFormVisible = false;
      },
      initData() { //初始化数据
        agentHttp.get(`adminApi/admin/mortgage_order/first_order_detail/${this.mortgageOrderExtendId}`).then((data) => {
          this.tableData = Object.assign({}, data.data, this.tableData)
          //青牛的图片
          qiniuArray( this.tableData)
        })
      },
      takeData() { // 取出数据传递给后台
        const params = {
          mortgageOrderExtendId: this.tableData.id,
          resourceAuditOpinion: this.tableData.resourceAuditOpinion,
          types: []          
        };
        this.tableData.carTypes.map((item, index) => {
          return params.types.push({
            mortgageOrderDetailId: item.id,
            carMortgageMoney: item.carMortgageMoney
          })

        })

        return params

      },
      inspect(code,data){     //验证
        var type = decimalTwo ;
          return muchVerification(code,data,type)
      },
    },
    props: {
      dialogSendData: {
        type: Object
      },
      mortgageOrderExtendId: {
        type: Number
      }
    },
    mounted() {
      this.initData()
      console.dir(VM)
    },
    components: {
      qiniu
    }
  };

</script>

<style lang="scss" scoped>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }

  .clearfix:after {
    clear: both;
  }

  .box-card {
    margin-top: 20px;
  }

  .align {
    margin: 50px auto 0;
    text-align: center;
  }

</style>

